package Hot100.Tree;

import java.util.*;

public class levelOrder {
    public static void main(String[] args) {
        int[] value = {1,2,3,4,5};
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.insert(value);
        List<List<Integer>> list = levelOrder(binaryTree.root);
        System.out.println(list);
    }
    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        if(root == null)
            return list;
        Queue<TreeNode> queue = new LinkedList<>() ;

        List<Integer> temp = new ArrayList<>();
        temp.add(root.val);
        queue.offer(root);
        list.add(temp);
        while (!queue.isEmpty()){
            int n = queue.size();
            temp = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                TreeNode current = queue.poll();
                if (current.left != null) {
                    queue.offer(current.left);
                    temp.add(current.left.val);
                }
                if (current.right != null) {
                    queue.offer(current.right);
                    temp.add(current.right.val);
                }
            }
            if(temp.size() != 0)
                list.add(temp);

        }
        return list;

    }
}
